home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / test / colbrowser.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  8.3 KB  |  345 lines

  1. //
  2. // "$Id: colbrowser.cxx,v 1.5 1999/01/07 19:17:51 mike Exp $"
  3. //
  4. // Forms test program for the Fast Light Tool Kit (FLTK).
  5. //
  6. // This is an XForms program from the 0.86 distribution of XForms.
  7. // It has been modified as little as possible to work under fltk by
  8. // using fltk's Forms emulation.  Search for "fltk" to find all the
  9. // changes
  10. //
  11. // Copyright 1998-1999 by Bill Spitzak and others.
  12. //
  13. // This library is free software; you can redistribute it and/or
  14. // modify it under the terms of the GNU Library General Public
  15. // License as published by the Free Software Foundation; either
  16. // version 2 of the License, or (at your option) any later version.
  17. //
  18. // This library is distributed in the hope that it will be useful,
  19. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  21. // Library General Public License for more details.
  22. //
  23. // You should have received a copy of the GNU Library General Public
  24. // License along with this library; if not, write to the Free Software
  25. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  26. // USA.
  27. //
  28. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  29. //
  30.  
  31. #include <FL/forms.H>    // changed for fltk
  32. #include <stdlib.h>
  33. #include <stdio.h>    // added for fltk
  34. #include <string.h>    // added for fltk
  35.  
  36. #define MAX_RGB 3000
  37.  
  38. static FL_FORM *cl;
  39. static Fl_Widget *rescol, *dbobj, *colbr, *rs, *gs, *bs;
  40. char dbname[FL_PATH_MAX];
  41. static void create_form_cl(void);
  42. static int load_browser(char *);
  43.  
  44. /* the RGB data file does not have a standard location on unix. */
  45.  
  46. #ifdef __VMS
  47.   static char *rgbfile = "SYS$MANAGER:DECW$RGB.DAT";
  48. #else
  49. #ifdef __EMX__   /* OS2 */
  50.   static char *rgbfile = "/XFree86/lib/X11/rgb.txt";
  51. #else
  52. #ifdef __FreeBSD__
  53.   static char *rgbfile = "/usr/X11R6/lib/X11/rgb.txt";
  54. #else
  55.    static char *rgbfile = "/usr/lib/X11/rgb.txt";
  56. #endif
  57. #endif
  58. #endif
  59.  
  60. typedef struct { int r, g, b; } RGBdb;
  61.  
  62. static RGBdb rgbdb[MAX_RGB];
  63.  
  64. int
  65. main(int argc, char *argv[])
  66. {
  67.  
  68.     fl_initialize(&argc, argv, "FormDemo", 0, 0);
  69.  
  70.     create_form_cl();
  71.     strcpy(dbname, rgbfile);
  72.  
  73.     if (load_browser(dbname))
  74.     fl_set_object_label(dbobj, dbname);
  75.     else
  76.     fl_set_object_label(dbobj, "None");
  77.  
  78. //    fl_set_form_minsize(cl, cl->w , cl->h); // removed for fltk
  79. //    fl_set_form_maxsize(cl, 2*cl->w , 2*cl->h); // removed for fltk
  80.     cl->size_range(cl->w(),cl->h(),2*cl->w(),2*cl->h()); // added for fltk
  81.     // border changed from FL_TRANSIENT for fltk:
  82.     // This is so Esc & the close box will close the window.
  83.     // (on transient windows attempting to close it just calls the callback)
  84.     fl_show_form(cl, FL_PLACE_FREE, 1/*FL_TRANSIENT*/, "RGB Browser");
  85.  
  86.  
  87.     while (fl_do_forms())
  88.     ;
  89.     return 0;
  90. }
  91.  
  92. static void
  93. set_entry(int i)
  94. {
  95.     RGBdb *db = rgbdb + i;
  96.  
  97.     fl_freeze_form(cl);
  98. // unclear why demo is doing this.  This messes up FL:
  99. //    fl_mapcolor(FL_FREE_COL4+i, db->r, db->g, db->b);
  100.     fl_mapcolor(FL_FREE_COL4, db->r, db->g, db->b);
  101.     fl_set_slider_value(rs, db->r);
  102.     fl_set_slider_value(gs, db->g);
  103.     fl_set_slider_value(bs, db->b);
  104.     fl_redraw_object(rescol);
  105.     fl_unfreeze_form(cl);
  106. }
  107.  
  108. static void
  109. br_cb(Fl_Widget * ob, long)
  110. {
  111.     int r = fl_get_browser(ob);
  112.  
  113.     if (r <= 0)
  114.     return;
  115.     set_entry(r - 1);
  116. }
  117.  
  118. static int
  119. read_entry(FILE * fp, int *r, int *g, int *b, char *name)
  120. {
  121.     int  n;
  122.     char buf[512], *p;
  123.  
  124.     if (!fgets(buf, sizeof(buf) - 1, fp))
  125.       return 0;
  126.  
  127.     if(buf[0] == '!')
  128.       fgets(buf,sizeof(buf)-1,fp);
  129.  
  130.     if(sscanf(buf, " %d %d %d %n", r, g, b, &n) < 3)
  131.     return 0;
  132.  
  133.     p = buf + n;
  134.  
  135.     /* squeeze out all spaces */
  136.     while (*p)
  137.     {
  138.     if (*p != ' ' && *p != '\n')
  139.         *name++ = *p;
  140.     p++;
  141.     }
  142.     *name = 0;
  143.  
  144.     return (feof(fp) || ferror(fp)) ? 0 : 1;
  145. }
  146.  
  147.  
  148. static int
  149. load_browser(char *fname)
  150. {
  151.     FILE *fp;
  152.     RGBdb *db = rgbdb, *dbs = db + MAX_RGB;
  153.     int r, g, b,  lr  = -1 , lg = -1, lb = -1;
  154.     char name[256], buf[256];
  155. #ifdef __EMX__
  156.     if (!(fp = fopen(__XOS2RedirRoot(fname), "r")))
  157. #else
  158.     if (!(fp = fopen(fname, "r")))
  159. #endif
  160.     {
  161.     fl_show_alert("Load", fname, "Can't open", 0);
  162.     return 0;
  163.     }
  164.  
  165.     /* read the items */
  166.  
  167.     fl_freeze_form(cl);
  168.  
  169.     for (; db < dbs && read_entry(fp, &r, &g, &b, name);)
  170.     {
  171.     db->r = r;
  172.     db->g = g;
  173.     db->b = b;
  174.  
  175.     /* unique the entries on the fly */
  176.     if (lr != r || lg != g || lb != b)
  177.     {
  178.         db++;
  179.         lr = r;
  180.         lg = g;
  181.         lb = b;
  182.         sprintf(buf, "(%3d %3d %3d) %s", r, g, b, name);
  183.         fl_addto_browser(colbr, buf);
  184.     }
  185.     }
  186.     fclose(fp);
  187.  
  188.     if (db < dbs)
  189.     db->r = 1000;        /* sentinel */
  190.     else
  191.     {
  192.     db--;
  193.     db->r = 1000;
  194.     }
  195.  
  196.     fl_set_browser_topline(colbr, 1);
  197.     fl_select_browser_line(colbr, 1);
  198.     set_entry(0);
  199.     fl_unfreeze_form(cl);
  200.     return 1;
  201. }
  202.  
  203. static int
  204. search_entry(int r, int g, int b)
  205. {
  206.     register RGBdb *db = rgbdb;
  207.     int i, j, diffr, diffg, diffb;
  208.     unsigned int diff, mindiff;
  209.  
  210.     mindiff = ~0;
  211.     for (i = j = 0; db->r < 256; db++, i++)
  212.     {
  213.        diffr = r - db->r;
  214.        diffg = g - db->g;
  215.        diffb = b - db->b;
  216.  
  217. #ifdef FL_LINEAR
  218.     diff = unsigned(3.0 * (FL_abs(r - db->r)) +
  219.             (5.9 * FL_abs(g - db->g)) +
  220.             (1.1 * (FL_abs(b - db->b)));
  221. #else
  222.         diff = unsigned(3.0 * (diffr *diffr) +
  223.             5.9 * (diffg *diffg) +
  224.             1.1 * (diffb *diffb));
  225. #endif
  226.  
  227.     if (mindiff > diff)
  228.     {
  229.         mindiff = diff;
  230.         j = i;
  231.     }
  232.     }
  233.  
  234.     return j;
  235. }
  236.  
  237. static void
  238. search_rgb(Fl_Widget *, long)
  239. {
  240.     int r, g, b, i;
  241.     int top  = fl_get_browser_topline(colbr);
  242.  
  243.     r = int(fl_get_slider_value(rs));
  244.     g = int(fl_get_slider_value(gs));
  245.     b = int(fl_get_slider_value(bs));
  246.  
  247.     fl_freeze_form(cl);
  248.     fl_mapcolor(FL_FREE_COL4, r, g, b);
  249.     fl_redraw_object(rescol);
  250.     i = search_entry(r, g, b);
  251.     /* change topline only if necessary */
  252.     if(i < top || i > (top+15))
  253.        fl_set_browser_topline(colbr, i-8);
  254.     fl_select_browser_line(colbr, i + 1);
  255.     fl_unfreeze_form(cl);
  256. }
  257.  
  258. /* change database */
  259. static void
  260. db_cb(Fl_Widget * ob, long)
  261. {
  262.     const char *p = fl_show_input("Enter New Database Name", dbname);
  263.     char buf[512];
  264.  
  265.     if (!p || strcmp(p, dbname) == 0)
  266.     return;
  267.  
  268.     strcpy(buf, p);
  269.     if (load_browser(buf))
  270.     strcpy(dbname, buf);
  271.     else
  272.     fl_set_object_label(ob, dbname);
  273. }
  274.  
  275. static void
  276. done_cb(Fl_Widget *, long)
  277. {
  278.     exit(0);
  279. }
  280.  
  281. static void
  282. create_form_cl(void)
  283. {
  284.     Fl_Widget *obj;
  285.  
  286.     if (cl)
  287.     return;
  288.  
  289.     cl = fl_bgn_form(FL_NO_BOX, 330, 385);
  290.     obj = fl_add_box(FL_UP_BOX, 0, 0, 330, 385, "");
  291.     fl_set_object_color(obj, FL_INDIANRED, FL_COL1);
  292.  
  293.     obj = fl_add_box(FL_NO_BOX, 40, 10, 250, 30, "Color Browser");
  294.     fl_set_object_lcol(obj, FL_RED);
  295.     fl_set_object_lsize(obj, FL_HUGE_SIZE);
  296.     fl_set_object_lstyle(obj, FL_BOLD_STYLE + FL_SHADOW_STYLE);
  297.  
  298.     dbobj = obj = fl_add_button(FL_NORMAL_BUTTON, 40, 50, 250, 25, "");
  299.     fl_set_object_boxtype(obj, FL_BORDER_BOX);
  300.     fl_set_object_color(obj, /*fl_get_visual_depth()==1 ? FL_WHITE:*/ FL_INDIANRED,
  301.                         FL_INDIANRED);
  302.     fl_set_object_callback(obj, db_cb, 0);
  303.     rs = obj = fl_add_valslider(FL_VERT_FILL_SLIDER, 225, 130, 30, 200, "");
  304.     fl_set_object_color(obj, FL_INDIANRED, FL_RED);
  305.     fl_set_slider_bounds(obj, 0, 255);
  306.     fl_set_slider_precision(obj, 0);
  307.     fl_set_object_callback(obj, search_rgb, 0);
  308.     fl_set_slider_return(obj, 0);
  309.  
  310.     gs = obj = fl_add_valslider(FL_VERT_FILL_SLIDER, 255, 130, 30, 200, "");
  311.     fl_set_object_color(obj, FL_INDIANRED, FL_GREEN);
  312.     fl_set_slider_bounds(obj, 0, 255);
  313.     fl_set_slider_precision(obj, 0);
  314.     fl_set_object_callback(obj, search_rgb, 1);
  315.     fl_set_slider_return(obj, 0);
  316.  
  317.     bs = obj = fl_add_valslider(FL_VERT_FILL_SLIDER, 285, 130, 30, 200, "");
  318.     fl_set_object_color(obj, FL_INDIANRED, FL_BLUE);
  319.     fl_set_slider_bounds(obj, 0, 255);
  320.     fl_set_slider_precision(obj, 0);
  321.     fl_set_object_callback(obj, search_rgb, 2);
  322.     fl_set_slider_return(obj, 0);
  323.  
  324.  
  325.     colbr = obj = fl_add_browser(FL_HOLD_BROWSER, 10, 90, 205, 240, "");
  326.     fl_set_browser_fontstyle(obj, FL_FIXED_STYLE); 
  327.     fl_set_object_callback(obj, br_cb, 0);
  328.  
  329.  
  330.     obj = fl_add_button(FL_NORMAL_BUTTON, 135, 345, 80, 30, "Done");
  331.     fl_set_object_callback(obj, done_cb, 0);
  332.  
  333.     rescol = obj = fl_add_box(FL_FLAT_BOX, 225, 90, 90, 35, "");
  334.     fl_set_object_color(obj, FL_FREE_COL4, FL_FREE_COL4);
  335.     fl_set_object_boxtype(obj, FL_BORDER_BOX);
  336.  
  337.  
  338.     fl_end_form();
  339.     fl_scale_form(cl, 1.1, 1.0);
  340. }
  341.  
  342. //
  343. // End of "$Id: colbrowser.cxx,v 1.5 1999/01/07 19:17:51 mike Exp $".
  344. //
  345.